package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.base.BaseActivity;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.OrderDetail;
import com.example.yuejian.yuejian.bean.Tuikuan;
import com.example.yuejian.yuejian.model.UserModel;
import com.squareup.picasso.Picasso;

public class OrderActivity extends BaseActivity {
    private TextView order_state;
    private TextView order_name;
    private TextView order_price;
    private TextView order_place;
    private TextView order_time;
    private TextView order_num;
    private TextView order_allmoney;
    private TextView order_createtime;
    private TextView order_pay_type;
    private ImageView order_image;
    private Button btn_tuikuan;
    private ImageView btn_back;
    String orderid;
    private ModleListener<OrderDetail>listener=new ModleListener<OrderDetail>() {
        @Override
        public void onResponse(OrderDetail o) {
            OrderDetail res=o;
            if (res.getState()==0)
            {
                order_state.setText("待参加");
            }
            else
            {
                order_state.setText("已完成");
            }
            order_name.setText(res.getTrainname());
            order_price.setText(res.getTrainmoney()+"元");
            order_place.setText(res.getPlace());
            order_time.setText(res.getTrainTime());
            order_num.setText(res.getNum()+"");
            order_allmoney.setText(res.getMoney()+"元");
            order_createtime.setText(res.getCreatetime());
            order_pay_type.setText(res.getPay_type());
            if(!"".equals(res.getTrainphoto()))
            {
                Picasso.with(getApplication()).load(res.getTrainphoto()).into(order_image);
            }
            else {
                order_image.setImageResource(R.drawable.camera);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    Tuikuan res;
    private ModleListener<Tuikuan>tuikuanModleListener=new ModleListener<Tuikuan>() {
        @Override
        public void onResponse(Tuikuan o) {
             res=o;
            Toast.makeText(getApplication(),res.getRetMsg(),Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplication(),res.getRetMsg(),Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        initViews();
        initEvents();
        back();
    }
    private void initViews()
    {
       order_state= (TextView) findViewById(R.id.order_detail_stats);
        order_name= (TextView) findViewById(R.id.order_detail_name);
        order_price= (TextView) findViewById(R.id.order_detail_train_price);
        order_place= (TextView) findViewById(R.id.order_detail_place);
        order_time= (TextView) findViewById(R.id.order_detail_time);
        order_num= (TextView) findViewById(R.id.order_detail_num);
        order_allmoney= (TextView) findViewById(R.id.order_detail_allmoney);
        order_createtime= (TextView) findViewById(R.id.order_detail_createTime);
        order_pay_type=(TextView) findViewById(R.id.order_detail_pay_type);
         order_image= (ImageView) findViewById(R.id.order_detail_image);
        btn_tuikuan= (Button) findViewById(R.id.order_detail_tuikuan);
    }
    private void getData()
    {
        Intent intent=getIntent();
        orderid=intent.getStringExtra("order_id");
        UserModel model=new UserModel();
        model.OrderDetail(orderid,listener);
    }
    private void initEvents()
    {
        btn_tuikuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserModel model=new UserModel();
                model.Tuikuan(getUser_id(),orderid,tuikuanModleListener);
               Intent intent=new Intent(OrderActivity.this,MyOrderActivity.class);
                startActivity(intent);
            }
        });
    }
    private void back()
    {
        btn_back=(ImageView)findViewById(R.id.imgBtn_order_detail_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    protected void onResume() {
        getData();
        super.onResume();
    }
}
